use std::{fmt::Display, path::PathBuf};

// use tauri::api::dialog::MessageDialogButtons;

/// 打开选择文件对话框
pub fn open_file_dialog(title: &str, file_name: &str) -> Option<PathBuf> {
    let result = tauri::api::dialog::blocking::FileDialogBuilder::new()
        .set_title(title)
        .set_file_name(file_name)
        .save_file();
    // if let Some(path) = result.as_ref() {
    //     if path.exists() && path.is_file() {
    //         if !tauri::api::dialog::blocking::MessageDialogBuilder::new(
    //             "提示",
    //             "该文件已经存在，确定要覆盖吗",
    //         )
    //         .buttons(MessageDialogButtons::OkCancel)
    //         .show()
    //         {
    //             return None;
    //         }
    //     }
    // }
    return result;
}

/// 转换成postgresql array 格式的字符串
///
/// 例如 ` { 1 , 2} `
pub fn cast_arrry_to_string<T: Display>(datas: Vec<T>, is_str: bool) -> String {
    let mut result = String::new();
    let mut index = 0;
    result.push('{');
    if is_str {
        for data in datas {
            if index != 0 {
                result.push(',');
            }
            result.push_str(&format!("\"{}\"", &data.to_string().replace('"', "\\\"")));
            index = index + 1;
        }
    } else {
        for data in datas {
            if index != 0 {
                result.push(',');
            }
            result.push_str(&data.to_string());
            index = index + 1;
        }
    }
    result.push('}');
    result
}
